﻿using System.Collections;
using System.Collections.Generic;


namespace DailyFx
{
	public class MathVector : IVector
	{
		private decimal[] mFields;


		public MathVector ( decimal[] src, bool copySrc = false)
		{
			if (copySrc == true)
			{
				mFields = new decimal[src.Length];
			}
			else
			{
				mFields = src; // keep referenec only .
			}
		}

		public MathVector ( int rank )
		{
			mFields = new decimal[ rank];
		}


		public decimal this [ int x ]
		{
			get
			{
				return mFields [ x];
			}
			set
			{
				mFields[x] = value;
			}
		}

		public static MathVector operator + ( MathVector x, MathVector y )
		{
			// assert( x.Rank == y.Rank );

			var r = new MathVector(x.Rank);

			for (int i = 0; i < x.Rank; i++)
				r[i] = x[i] + y[i];

			return r;
		}


		public static MathVector operator - ( MathVector x, MathVector y )
		{
			// assert(x.Rank == y.Rank);

			var r = new MathVector(x.Rank);

			for (int i = 0; i < x.Rank; i++)
				r[i] = x[i] - y[i];

			return r;
		}


		public static MathVector operator * ( MathVector x, decimal s )
		{
			var r = new MathVector (x.Rank) ;

			for (int i = 0; i < x.Rank; i++)
				r[i] = x[i] * s;

			return r;
		}

		public static implicit operator decimal [] ( MathVector x )
		{
			return x.mFields;
		}
		public static explicit operator MathVector ( decimal[] p )
		{
			return new MathVector( p, false);
		}

		public int Rank
		{
			get { return mFields.Length; }
		}

		public int Length 
		{
			get { return mFields.Length; } 
		}


		public IEnumerator<decimal> GetEnumerator ()
		{
			for (int i = 0; i < Rank; ++i)
			{
				yield return mFields[i];
			}
		}

		IEnumerator IEnumerable.GetEnumerator ()
		{
			for (int i = 0; i < Rank; ++i)
			{
				yield return mFields[ i];
			}
		}

	}

}
